/*
 * version May 14, 2011 Harman created constructor and parse method
 * version May 21, 2011 Harman changed days to ClassDays
 * version May 22, 2011 Gavin changed the types of various collections from List to Set to reflect
 * 						new parameters. 
 * version May 27, 2011 Harman added isTBA method and createSlot
 * version June 01, 2011 Harman changed theFile type from Readable to File
 */
package parsers;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import data.ClassDays;
import data.ClassSlot;
import data.TimeSlot;

/**
 * Parses a .csv schedule file and creates
 * a list of ClassSlots and TimeSlots
 * @author Harman
 * @version June 01, 2011
 */
public class ScheduleParser 
{
	
	private static final String SEPARATOR = ",";
	public static final String TBA = "TBA";
	private int myStartTime;
	private int myEndTime;
	public int myClassCredits;
	private char myClassSection;
	public String myClassNum;
	public String myClassTeacher;
	public String myClassTitle;
	public String fileName;
	private List<ClassSlot> mySlotList;
	private List<TimeSlot> myTimeSlotList;
	
	/**
	 * Parses a .csv schedule file
	 * @pre theFile is formatted appropriately. Specifically 
	 * each line is of the form:
	 * class number,class section,class title,class teacher,
	 * class days ex: (MW),int start time,int end time, class credits
	 * @pre theFile != null
	 * @post finds the file name, creates a list of ClassSlots and
	 * TimeSlots
	 * @param theFile
	 * @throws IOException
	 */
	public ScheduleParser(final String theFile) throws IOException {
		mySlotList = new ArrayList<ClassSlot>();
		myTimeSlotList = new ArrayList<TimeSlot>();
		startParser(theFile);
	}
	
	/**
	 * @param theFile
	 * @throws IOException
	 */
	private void startParser(final String theFile) throws IOException {
		try {
			BufferedReader br = new BufferedReader( new FileReader(theFile));
			String strLine = "";
			
			while( (strLine = br.readLine()) != null) {
				Set<ClassDays> days = new HashSet<ClassDays>();
				StringTokenizer strToken = new StringTokenizer(strLine, SEPARATOR);
				String current;
				if (strLine.charAt(0) == 'T') {
					
					if (!isTBA(current = strToken.nextToken(), 0))
						myClassNum = current;
					
					if (!isTBA(current = strToken.nextToken(), 1))
						myClassSection = current.charAt(0);
					
					if (!isTBA(current = strToken.nextToken(), 2))
						myClassTitle = current;
					
					if (!isTBA(current = strToken.nextToken(), 3))
						myClassTeacher = current;
					
					if (!isTBA(current = strToken.nextToken(), 4)) {
						for (int i = 0; i < current.length(); i++) {
							days.add(ClassDays.findDay(current.charAt(i)));
						}
					}
					
					if (!isTBA(current = strToken.nextToken(), 5))
						myStartTime = Integer.parseInt(current);
					
					if (!isTBA(current = strToken.nextToken(), 6))
						myEndTime = Integer.parseInt(current);
					
					if (Character.isDigit(strLine.charAt(strLine.length() - 1))) {
						myClassCredits = Character.getNumericValue(strLine.charAt(strLine.length() - 1));
					} else {
						myClassCredits = 0;
					}
					
					createSlot(days);
				}
			}
		}
		
		catch(Exception e) {
			throw new IOException("Exception while reading file: " + e);			
		}
	}
	
	/**
	 * Checks is the given string equals "TBA"
	 * @param the_string to be compared with "TBA"
	 * @param the_change the corresponding field
	 * @return true if the given string equals "TBA"
	 */
	public boolean isTBA(final String the_string, final int the_change) {
		boolean result = false;
		String str = the_string;
		int change = the_change;
		
		if ((str).equals(TBA)) {
			switch (change) {
				case 0:
					myClassNum = TBA;
					break;
				case 1:
					myClassSection = ' ';
					break;
				case 2:
					myClassTitle = TBA;
					break;
				case 3:
					myClassTeacher = TBA;
					break;
				case 4:
					break;
				case 5:
					myStartTime = 0;
					break;
				case 6:
					myEndTime = 0;
					break;
				default:
					break;
					
			}
			result = true;
		}
		return result;
	}
	
	/**
	 * Creates a ClassSlot with the saved parameters.
	 * Then adds the ClassSlot to the ArrayList.
	 */
	private void createSlot(final Set<ClassDays> the_days) {
		Set<ClassDays> days = the_days;
		TimeSlot tSlot = new TimeSlot(days, myStartTime, myEndTime);
		myTimeSlotList.add(tSlot);
		ClassSlot cSlot = new ClassSlot(tSlot, myClassNum, myClassTitle, 
				myClassSection, myClassTeacher, myClassCredits);
		mySlotList.add(cSlot);
	}
	
	public String getFileName() { return fileName; }
	public List<ClassSlot> getClassSlotList() { return mySlotList; }
	public List<TimeSlot> getTimeSlotList(){ return myTimeSlotList; }
}